//
// Created by vcq on 21/2/10.
//
#pragma once

#include "dbng.hpp"
#include "utils_dao.hpp"
#include <string>

using namespace nlohmann;
using namespace std;
using Dao = dao_t<dbng<mysql>>;
struct flow_now {
    int id;
    int plan;
    int plan_id;
    int pass;
    int user_id;
    string note;
};
REFLECTION(flow_now, id, plan, plan_id, pass, user_id, note)

json MGetFlowNow(basic_string_view<char> id,
                 basic_string_view<char> plan, basic_string_view<char> plan_id,
                 basic_string_view<char> pass, basic_string_view<char> user_id) {
    string sql = "select * from `flow_now` where 1=1 ";
    if (!id.empty()) {
        sql += "and id =";
        sql += id;
    }
    if (!plan.empty() && !plan_id.empty()) {
        sql += " and plan =";
        sql += plan;
        sql += " and plan_id=";
        sql += plan_id;
    }
    if (!pass.empty()) {
        sql += " and pass =";
        sql += pass;
    }
    if (!user_id.empty()) {
        sql += " and user_id ='";
        sql += user_id;
        sql += "'";
    }
    Dao dao;
    auto v = dao.query<flow_now>(sql);
    nlohmann::json article_list, ret, item;
    for (auto &o : v) {
        item["id"] = o.id;
        item["plan"] = o.plan;
        item["plan_id"] = o.plan_id;
        item["pass"] = o.pass;
        item["user_id"] = o.user_id;
        item["note"] = o.note;
        article_list.push_back(item);
    }
    ret["code"] = 0;
    ret["count"] = article_list.size();
    ret["data"] = article_list;
    return ret;
}

void MAddFlowNow(const struct flow_now &flow_now1) {
    Dao dao;
    dao.update_object(flow_now1);
}

void MDeleteFlowNow(basic_string_view<char> plan_id, basic_string_view<char> id) {
    Dao dao;
    string sql = "delete FROM `";
    sql += "flow_now";
    sql += "` where 1=1 ";
    if (!plan_id.empty() && !id.empty()) {
        sql += "and plan_id=";
        sql += plan_id.data();
        sql += "and id=";
        sql += id.data();
        dao.execute(sql);
    }
}